$(function () {
    // 商品详细信息
    var GoodsObj
    init();

    function init() {

        getDetail()
        eventlist();
    }

    function getDetail() {
        // http://api.pyg.ak48.xyz/api/public/v1/goods/detail
        $.ajax({
            type: "get",
            url: "goods/detail",
            data: {
                "goods_id": $.getValue("goods_id")
            },
            dataType: "json",
            success: function (res) {
                console.log(res);
                GoodsObj = res.data;
                var str = template("getDetail", {
                    "data": res.data
                });
                $(".pyg_view").html(str);
                var gallery = mui('.mui-slider');
                gallery.slider({
                    interval: 2000 //自动轮播周期，若为0则不自动播放，默认为0；
                });
            }
        })
    }

    function eventlist() {
        // 给加入购物车添加点击事件

        $(".add_btn").on("tap", function () {

            // 判断永久存储中有没有userinfo
            if (!$.checkLogin()) {
                // 没有用户信息 肯定未登录过
                mui.toast("未登录");
                sessionStorage.setItem("pageName", location.href);
                setTimeout(function () {
                    location.href = "/pages/login.html";
                }, 1000);
                return;
            }
            // console.log(123);
            // 商品的对象
            var goodsObj = {
                cat_id: GoodsObj.cat_id,
                goods_id: GoodsObj.goods_id,
                goods_name: GoodsObj.goods_name,
                goods_number: GoodsObj.goods_number,
                goods_price: GoodsObj.goods_price,
                goods_weight: GoodsObj.goods_weight,
                goods_small_logo: GoodsObj.goods_small_logo
            }
            // 获取tokenI
            var token = JSON.parse(localStorage.getItem("userInfo")).token;
            console.log(token);
            // 发送ajax请求
            $.ajax({
                type: "post",
                url: "my/cart/add",
                data: {
                    "info": JSON.stringify(goodsObj)
                },
                headers: {
                    "Authorization": token
                },
                dataType: "json",
                success: function (res) {
                    console.log(res);
                    sessionStorage.setItem("pageName", location.href);
                    // debugger
                    // 如果没登录返回到登录页面
                    if (res.meta.status == 401) {
                        mui.toast("未登录");
                        // 1秒后跳转页面
                        setTimeout(function () {
                            location.href = "/pages/login.html";
                        }, 1000)
                    } else if (res.meta.status == 200) {
                        mui.confirm("是否留在当前页面", "添加成功", ["跳转", "取消"], function (etype) {
                            if (etype.index == 0) {
                                mui.toast("添加成功");
                                // 1秒后跳转页面

                                setTimeout(function () {
                                    location.href = "/pages/cart.html";
                                }, 1000)
                            } else if (etype.index == 1) {
                                // 取消就不跳转页面了
                            }
                        });
                    }

                }
            })

        })
    }
})